﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web;

namespace DR5.SPExtend.Controls
{
    class SPXGridView : SPGridView
    {
        SPList _list = null;
        string _viewName = string.Empty;

        public SPXGridView(SPList list, string viewName)
        {
            this.AutoGenerateColumns = false;
            this._list = list;
            this._viewName = viewName;
            GenerateColumns();
        }

        public string SourceUrl { private get; set; }

        private void GenerateColumns()
        {
            SPViewFieldCollection viewFields = this._list.Views[_viewName].ViewFields;
            
            foreach (string fieldName in viewFields)
            {
                try
                {
                    if (fieldName == "LinkFilename")
                    {
                        continue;
                    }
                    SPField field = _list.Fields.GetField(fieldName);
                    SPBoundField boundField = new SPBoundField();
                    boundField.HeaderText = field.Title;
                    boundField.DataField = fieldName;
                    boundField.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Justify;
                    boundField.ItemStyle.Wrap = true;
                    this.Columns.Add(boundField);
                }
                catch
                {
                }
            }
        }

        public void AddDisplayLinkColumn()
        {
            string dispFormUrl = _list.ParentWebUrl + "/" + _list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
            string navigateUrlDisp = _list.ParentWeb.Site.MakeFullUrl(dispFormUrl) + "?ID={0}&Source="+ HttpUtility.UrlEncode(SourceUrl);

            SPXHyperLinkField fldDispLink = new SPXHyperLinkField();
            fldDispLink.HeaderText = "Display";
            fldDispLink.Text = "View Item";
            fldDispLink.DataFieldsForNavigateUrl = new string[] { "ID" };
            fldDispLink.NavigateUrl = navigateUrlDisp;
            //fldDispLink.NavigateUrl = "javascript:void(0);";

            string jsfunctionDisp = "javascript:DisplayItemPopup('" + navigateUrlDisp + "','" + _list.Title + "');javascript:return false;";
            fldDispLink.OnClickString = jsfunctionDisp;
            fldDispLink.DataFieldsForClickString = new string[] { "ID" };

            this.Columns.Add(fldDispLink);

            //SPBoundField fldDisplayLink = new SPBoundField();
            //fldDisplayLink.HeaderText = "Display";
            //fldDisplayLink.DataField = "ID";
            //this.Columns.Add(fldDisplayLink);
        }

        public void AddEditLinkColumn()
        {
            string editFormUrl = _list.ParentWebUrl + "/" + _list.Forms[PAGETYPE.PAGE_EDITFORM].Url;
            string navigateUrlEdit = _list.ParentWeb.Site.MakeFullUrl(editFormUrl) + "?ID={0}&Source="+ HttpUtility.UrlEncode(SourceUrl);

            SPXHyperLinkField fldEditLink = new SPXHyperLinkField();
            fldEditLink.HeaderText = "Edit";
            fldEditLink.Text = "Edit Item";
            fldEditLink.DataFieldsForNavigateUrl = new string[] { "ID" };
            fldEditLink.NavigateUrl = navigateUrlEdit;
            //fldEditLink.NavigateUrl = "javascript:void(0);";

            string jsfunctionEdit = "javascript:EditItemPopup('" + navigateUrlEdit + "','" + _list.Title + "');javascript:return false;";
            fldEditLink.OnClickString = jsfunctionEdit;
            fldEditLink.DataFieldsForClickString = new string[] { "ID" };

            this.Columns.Add(fldEditLink);
        }

        public void AddDeleteLinkColumn()
        {

        }

        public void BindData(DataTable dt)
        {
            this.DataSource = dt;
            this.DataBind();
        }
    }
}
